<template>
  <div>
    <common-head title="确认订单"></common-head>
    <div class="wrap">
      <van-contact-card
        :type="switchType"
        :name="linkman.linkMan"
        :tel="linkman.mobile"
        @click="onEdit"
      />
     <div class="item" v-for="item in items" :key="item.id">
        <van-card
        :num="item.number"
        :price="item.price"
        :title="item.name"
        :thumb="item.pic"
      />
     </div>
      <van-submit-bar :price="total*100" button-text="提交订单" @submit="onSubmit" />
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { getCart, defaultLinkman } from '@api'
export default {
  components: {
    CommonHead
  },
  data () {
    return {
      linkman: {},
      items: [],
      total: 0
    }
  },
  created () {
    this.getItems()
    this.getLinkman()
  },
  methods: {
    getItems () {
      getCart({
        token: localStorage.getItem('token')
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.items.filter(item => item.selected)
          this.total = res.data.data.price
        }
      })
    },
    getLinkman () {
      if (this.$route.query.id) {
        defaultLinkman({
          id: this.$route.query.id,
          token: localStorage.getItem('token')
        }).then(res => {
          console.log(res)
          if (res.data.code === 0) {
            this.linkman = res.data.data.info
          }
        })
      } else {
        defaultLinkman({
          token: localStorage.getItem('token')
        }).then(res => {
          console.log(res)
          if (res.data.code === 0) {
            this.linkman = res.data.data.info
          }
        })
      }
    },
    onEdit () {
      this.$router.push('/showLinkman')
    },
    onSubmit () {
      alert('支付完成')
    },
    onAdd () {}
  },
  computed: {
    switchType () {
      return this.linkman.id ? 'edit' : 'add'
    }
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top: 50px;
  .item{
    padding-bottom: 50px;
  }
}
</style>
